//
//  UIView+HBCategory.m
//  HBLibary
//
//  Created by Rambo on 2018/10/26.
//

#import "UIView+HBCategory.h"
#import "UIDevice+HBCategory.h"
#import "NSObject+HBCategory.h"
#import <objc/runtime.h>

@interface UIView ()

/** 画线属性*/
@property (nonatomic, strong) NSMutableArray *hb_borderLineLayers;
@property (nonatomic, assign) NSInteger hb_borderLineOption;
@property (nonatomic, strong) NSNumber *hb_borderLineWidth;

/** 切圆角属性*/
@property (nonatomic, strong) NSNumber *hb_radius;
@property (nonatomic, assign) UIRectCorner hb_rectCorner;
@property (nonatomic, assign) BOOL hb_isMaskLayer;

@end

@implementation UIView (HBCategory)

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [NSObject hb_swizzlingInstanceMethodWithClass:self originSel:@selector(layoutSubviews) newSel:@selector(UIView_HBCategory_layoutSubviews)];
    });
}

+ (CGFloat)hb_getStatusHeight{
    return [UIApplication sharedApplication].statusBarFrame.size.height;
}

+ (CGFloat)hb_getNavheight{
    return [self hb_getStatusHeight] + 44;
}

+ (CGFloat)hb_getTabbarHeight{
    
    static CGFloat height = 49;
    if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
        return height;
    }else{
        if ([UIDevice hb_isiPhoneX ]) {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                height += 34;
            });
            
        }
    }
    return height;
}

- (void)UIView_HBCategory_layoutSubviews{
    [self UIView_HBCategory_layoutSubviews];
    
    [self drawBorderLine];
    [self hb_deawCorner];
}


#pragma mark - 画圆角

- (void)hb_deawCorner{
    if (self.hb_isMaskLayer) {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:self.hb_rectCorner cornerRadii:CGSizeMake(self.hb_radius.floatValue,self.hb_radius.floatValue)];//圆角大小
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }
}

/**
 设置上圆角
 
 @param radius 圆角大小
 */
- (void)hb_setTopCornerWithRadius:(CGFloat)radius{
     [self hb_setCornerWithRadius:radius rectCorner:(UIRectCornerTopLeft | UIRectCornerTopRight)];
}

/**
 设置下圆角
 
 @param radius 圆角大小
 */
- (void)hb_setBottomCornerWithRadius:(CGFloat)radius{
     [self hb_setCornerWithRadius:radius rectCorner:(UIRectCornerBottomLeft | UIRectCornerBottomRight)];
}

/**
 设置全圆角
 
 @param radius 圆角大小
 */
- (void)hb_setAllCornerWithRadius:(CGFloat)radius{
    [self hb_setCornerWithRadius:radius rectCorner:(UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight)];
}

/**
 设置左圆角
 
 @param radius 圆角大小
 */
- (void)hb_setLeftCornerWithRadius:(CGFloat)radius{
    [self hb_setCornerWithRadius:radius rectCorner:(UIRectCornerTopLeft | UIRectCornerBottomLeft)];
}

/**
 设置右圆角
 
 @param radius 圆角大小
 */
- (void)hb_setRightCornerWithRadius:(CGFloat)radius{
    [self hb_setCornerWithRadius:radius rectCorner:(UIRectCornerTopRight | UIRectCornerBottomRight)];
}

/**
 设置圆角
 
 @param radius 圆角大小
 @param rectCorner 圆角位置
 */
- (void)hb_setCornerWithRadius:(CGFloat)radius rectCorner:(UIRectCorner)rectCorner{
    self.hb_isMaskLayer = YES;
    self.hb_radius = @(radius);
    self.hb_rectCorner = rectCorner;
}

- (void)setHb_isMaskLayer:(BOOL)hb_isMaskLayer{
    objc_setAssociatedObject(self, @selector(hb_isMaskLayer), @(hb_isMaskLayer), OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)hb_isMaskLayer{
    return [objc_getAssociatedObject(self, @selector(hb_isMaskLayer)) boolValue];
}


- (void)setHb_radius:(NSNumber *)hb_radius{
    objc_setAssociatedObject(self, @selector(hb_radius), hb_radius, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSNumber *)hb_radius{
    return objc_getAssociatedObject(self, @selector(hb_radius));
}

- (void)setHb_rectCorner:(UIRectCorner)hb_rectCorner{
    objc_setAssociatedObject(self, @selector(hb_rectCorner), @(hb_rectCorner), OBJC_ASSOCIATION_ASSIGN);
}


- (UIRectCorner)hb_rectCorner{
    return [objc_getAssociatedObject(self, @selector(hb_rectCorner)) unsignedIntegerValue];
}


#pragma mark - 周边画线

- (void)drawBorderLine{
    if (self.hb_borderLineLayers && self.hb_borderLineLayers.count > 0) {
        NSInteger index = 0;
        if ([self isExistLineInOption:UIViewBorderLineOptionTop]) {
            CALayer *layer = self.hb_borderLineLayers[index];
//            [self.layer insertSublayer:layer atIndex:0];
            [self.layer addSublayer:layer];
            layer.frame = CGRectMake(0, 0, self.frame.size.width, self.hb_borderLineWidth.floatValue);
            index++;
        }
        
        if ([self isExistLineInOption:UIViewBorderLineOptionLeft]) {
            CALayer *layer = self.hb_borderLineLayers[index];
//            [self.layer insertSublayer:layer atIndex:0];
            [self.layer addSublayer:layer];
            layer.frame = CGRectMake(0, 0, self.hb_borderLineWidth.floatValue, self.frame.size.height);
            index++;
        }
        
        if ([self isExistLineInOption:UIViewBorderLineOptionBottom]) {
            CALayer *layer = self.hb_borderLineLayers[index];
//            [self.layer insertSublayer:layer atIndex:0];
            [self.layer addSublayer:layer];
            layer.frame = CGRectMake(0, self.frame.size.height - self.hb_borderLineWidth.floatValue, self.frame.size.width, self.hb_borderLineWidth.floatValue);
            index++;
        }
        
        if ([self isExistLineInOption:UIViewBorderLineOptionRight]) {
            CALayer *layer = self.hb_borderLineLayers[index];
//            [self.layer insertSublayer:layer atIndex:0];
            [self.layer addSublayer:layer];
            layer.frame = CGRectMake(self.frame.size.width - self.hb_borderLineWidth.floatValue, 0, self.hb_borderLineWidth.floatValue, self.frame.size.height);
            index++;
        }
    }
}

/**
 在周边画线
 
 @param option 线的位置
 @param width 线的宽度
 @param color 线的颜色
 */
- (void)hb_drawBorderLineAtOption:(UIViewBorderLineOption)option width:(CGFloat)width color:(UIColor*)color{
   
    if (self.hb_borderLineLayers) {
        for (CALayer *layer in self.hb_borderLineLayers) {
            [layer removeFromSuperlayer];
        }
    }
    
    if (!color) color = [UIColor clearColor];
    
    self.hb_borderLineLayers = [NSMutableArray array];
    
    self.hb_borderLineWidth = @(width);
    self.hb_borderLineOption = option;
    
    if ([self isExistLineInOption:UIViewBorderLineOptionTop]) {
        CALayer *layer = [CALayer layer];
        layer.backgroundColor = color.CGColor;
        [self.hb_borderLineLayers addObject:layer];
    }
    
    if ([self isExistLineInOption:UIViewBorderLineOptionLeft]) {
        CALayer *layer = [CALayer layer];
        layer.backgroundColor = color.CGColor;
        [self.hb_borderLineLayers addObject:layer];
    }
    
    if ([self isExistLineInOption:UIViewBorderLineOptionBottom]) {
        CALayer *layer = [CALayer layer];
        layer.backgroundColor = color.CGColor;
        [self.hb_borderLineLayers addObject:layer];
    }
    
    if ([self isExistLineInOption:UIViewBorderLineOptionRight]) {
        CALayer *layer = [CALayer layer];
        layer.backgroundColor = color.CGColor;
        [self.hb_borderLineLayers addObject:layer];
    }
    
}

- (BOOL)isExistLineInOption:(UIViewBorderLineOption)option{
    return self.hb_borderLineOption & option;
}

- (void)setHb_borderLineLayers:(NSMutableArray *)hb_borderLineLayers{
    objc_setAssociatedObject(self, @selector(hb_borderLineLayers), hb_borderLineLayers, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}



- (NSMutableArray *)hb_borderLineLayers{
    return objc_getAssociatedObject(self, @selector(hb_borderLineLayers));
}

- (void)setHb_borderLineOption:(NSInteger)hb_borderLineOption{
    objc_setAssociatedObject(self, @selector(hb_borderLineOption), @(hb_borderLineOption), OBJC_ASSOCIATION_ASSIGN);
}

- (NSInteger)hb_borderLineOption{
    return [objc_getAssociatedObject(self, @selector(hb_borderLineOption)) integerValue];
}

- (void)setHb_borderLineWidth:(NSNumber *)hb_borderLineWidth{
    objc_setAssociatedObject(self, @selector(hb_borderLineWidth), hb_borderLineWidth, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSNumber *)hb_borderLineWidth{
    return objc_getAssociatedObject(self, @selector(hb_borderLineWidth));
}


/**
 设置虚线
 
 @param rect rect
 @param lineWidth 虚线宽度
 @param lineColor 虚线颜色
 */
- (void)hb_drawLineOfDashWithFrame:(CGRect)rect lineWidth:(CGFloat)lineWidth lineColor:(UIColor *_Nullable)lineColor{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:rect.origin];
    [path addLineToPoint:CGPointMake(rect.size.width, rect.origin.y)];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = lineColor.CGColor;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.lineWidth = lineWidth;
    shapeLayer.lineJoin = kCALineJoinMiter;
    shapeLayer.lineCap = kCALineCapSquare;
    shapeLayer.lineDashPattern = @[@3, @3];
    shapeLayer.path = path.CGPath;
    
    [self.layer addSublayer:shapeLayer];
}


@end
